﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;
using System.Windows.Navigation;
using GranitePackageSystem.GranitePackagingService;
using System.Collections.ObjectModel;
using GranitePackageSystem.Models;
using System.Collections.Specialized;
using GranitePackageSystem.Views.ChildWindows;

namespace GranitePackageSystem.Views.QA
{
    public partial class QualityAssuranceView : Page
    {
        private ObservableCollection<WorkLogEntry> workLog;

        private GranitePackagingService.Request currentRequest;
        private GranitePackagingService.Package packageData;
        private GranitePackagingService.Program programData;

        public QualityAssuranceView()
        {
            InitializeComponent();
        }
        #region init
        // Executes when the user navigates to this page.
        protected override void OnNavigatedTo(NavigationEventArgs e)
        {
            checkRoles();

            GranitePackagingServiceSoapClient proxy = new GranitePackagingServiceSoapClient();
            proxy.InnerChannel.OperationTimeout = new TimeSpan(0, 10, 0);

            if (App.Current.Resources.Contains("Current Request"))
            {
                currentRequest = App.Current.Resources["Current Request"] as GranitePackagingService.Request;

                if (currentRequest.Stage.Equals("QA"))
                {
                    busyIndicatorPackageInformation.IsBusy = true;

                    Web.User user = WebContext.Current.User;
                    if (!currentRequest.QAOwner.Equals(user.Name))
                    {
                        OwnershipWindow ownerWindow = new OwnershipWindow(currentRequest);
                        ownerWindow.Closed += new EventHandler(ownerWindow_Closed);
                        ownerWindow.Show();
                    }
                    else
                    {
                        proxy.getPackageListCompleted += new EventHandler<getPackageListCompletedEventArgs>(proxy_getPackageListCompleted_INIT);
                        proxy.getPackageListAsync(currentRequest);

                        proxy.getRequestWorkLogCompleted += new EventHandler<getRequestWorkLogCompletedEventArgs>(proxy_getRequestWorkLogCompleted_INIT);
                        proxy.getRequestWorkLogAsync(currentRequest);
                    }
                }
                else
                {
                    this.NavigationService.Navigate(new System.Uri("/Home", System.UriKind.Relative));
                }
            }
            else
            {
                this.NavigationService.Navigate(new System.Uri("/Home", System.UriKind.Relative));
            }
        }

        void ownerWindow_Closed(object sender, EventArgs e)
        {
            if (((OwnershipWindow)sender).DialogResult.Value.Equals(true))
            {
                GranitePackagingServiceSoapClient proxy = new GranitePackagingServiceSoapClient();
                proxy.InnerChannel.OperationTimeout = new TimeSpan(0, 10, 0);

                if (!currentRequest.Status.Contains("Pending"))
                {
                    currentRequest.Status = "Pending";
                    proxy.UnprovisionVirtualQAResourceAsync(currentRequest);
                    if (!btnTestWin7.IsEnabled)
                    {
                        toggleWin7Test();
                    }
                    else
                    {
                        toggleWinXPTest();
                    }
                    proxy.saveRequestCompleted += new EventHandler<saveRequestCompletedEventArgs>(proxy_saveRequestCompleted_OwnerChange_INIT);
                    proxy.saveRequestAsync(currentRequest);
                }
                else
                {
                    proxy.getPackageListCompleted += new EventHandler<getPackageListCompletedEventArgs>(proxy_getPackageListCompleted_INIT);
                    proxy.getPackageListAsync(currentRequest);

                    proxy.getRequestWorkLogCompleted += new EventHandler<getRequestWorkLogCompletedEventArgs>(proxy_getRequestWorkLogCompleted_INIT);
                    proxy.getRequestWorkLogAsync(currentRequest);
                }
            }
            else
            {
                this.NavigationService.Navigate(new System.Uri("/Home", System.UriKind.Relative));
            }
        }

        void proxy_saveRequestCompleted_OwnerChange_INIT(object sender, saveRequestCompletedEventArgs e)
        {
            Application.Current.Resources.Remove("Current Request");
            Application.Current.Resources.Add("Current Request", currentRequest);
            GranitePackagingServiceSoapClient proxy = new GranitePackagingServiceSoapClient();
            proxy.InnerChannel.OperationTimeout = new TimeSpan(0, 10, 0);

            proxy.getPackageListCompleted += new EventHandler<getPackageListCompletedEventArgs>(proxy_getPackageListCompleted_INIT);
            proxy.getPackageListAsync(currentRequest);

            proxy.getRequestWorkLogCompleted += new EventHandler<getRequestWorkLogCompletedEventArgs>(proxy_getRequestWorkLogCompleted_INIT);
            proxy.getRequestWorkLogAsync(currentRequest);
        }

        private void checkRoles()
        {
            Web.User user = WebContext.Current.User;
            bool userInARole = false;

            if (user.IsInRole("GENMILLS\\GPS_Administrators")) { userInARole = true; }
            if (user.IsInRole("GENMILLS\\GPS_DeploymentApprovers")) { userInARole = true; }

            if (userInARole) { }
            else { this.NavigationService.Navigate(new System.Uri("/Unauthorized", System.UriKind.Relative)); }
        }
        void proxy_getPackageListCompleted_INIT(object sender, getPackageListCompletedEventArgs e)
        {
            if (e.Result.Count > 0)
            {
                packageData = e.Result[0];

                boxPackageName.Text = packageData.Name.ToString();
                //boxPackageSize.Text = packageData.PackageSize_MB.ToString();
                boxPackageVersion.Text = packageData.Version.ToString();


                //Query Package for OS Compatibility Settings
                GranitePackagingServiceSoapClient proxy = new GranitePackagingServiceSoapClient();
                proxy.InnerChannel.OperationTimeout = new TimeSpan(0, 5, 0);

                proxy.getProgramListCompleted += new EventHandler<getProgramListCompletedEventArgs>(proxy_getProgramListCompleted_INIT);
                proxy.getProgramListAsync(e.Result[0]);
            }
            else
            {
                this.NavigationService.Navigate(new Uri("/Home", UriKind.Relative));
            }
        }
        void proxy_getProgramListCompleted_INIT(object sender, getProgramListCompletedEventArgs e)
        {
            if (e.Result.Count > 0)
            {
                programData = e.Result[0];
                if (programData.supportWin7_64)
                {
                    spinnerWin7.Visibility = System.Windows.Visibility.Collapsed;
                    imgWin7NonCompat.Visibility = System.Windows.Visibility.Collapsed;
                    imgWin7Compat.Visibility = System.Windows.Visibility.Visible;
                }
                else
                {
                    spinnerWin7.Visibility = System.Windows.Visibility.Collapsed;
                    imgWin7NonCompat.Visibility = System.Windows.Visibility.Visible;
                    imgWin7Compat.Visibility = System.Windows.Visibility.Collapsed;
                }
                if (programData.supportWinXP_32)
                {
                    spinnerWinXP.Visibility = System.Windows.Visibility.Collapsed;
                    imgWinXPNonCompat.Visibility = System.Windows.Visibility.Collapsed;
                    imgWinXPCompat.Visibility = System.Windows.Visibility.Visible;
                }
                else
                {
                    spinnerWinXP.Visibility = System.Windows.Visibility.Collapsed;
                    imgWinXPNonCompat.Visibility = System.Windows.Visibility.Visible;
                    imgWinXPCompat.Visibility = System.Windows.Visibility.Collapsed;
                }
                GranitePackagingServiceSoapClient proxy = new GranitePackagingServiceSoapClient();
                proxy.InnerChannel.OperationTimeout = new TimeSpan(0, 5, 0);
                proxy.getSCCMPackageSourceFileLocationCompleted += new EventHandler<getSCCMPackageSourceFileLocationCompletedEventArgs>(proxy_getSCCMPackageSourceFileLocationCompleted);
                proxy.getSCCMPackageSourceFileLocationAsync(packageData.Name, packageData.Publisher,packageData.Version);
            }
            else
            {
                this.NavigationService.Navigate(new Uri("/Home",UriKind.Relative));
            }
        }
        void proxy_getSCCMPackageSourceFileLocationCompleted(object sender, getSCCMPackageSourceFileLocationCompletedEventArgs e)
        {
            boxSourceFilesLocation.Text = e.Result;
            boxSourceFilesLocation.IsReadOnly = true;
            busyIndicatorPackageInformation.IsBusy = false;

            if (currentRequest.Status.Equals("Pending"))
            {
            }
            else
            {
                busyIndicatorMain.IsBusy = true;
                busyIndicatorMain.BusyContent = currentRequest.Status;
                GranitePackagingServiceSoapClient proxy = new GranitePackagingServiceSoapClient();
                proxy.InnerChannel.OperationTimeout = new TimeSpan(0, 5, 0);
                proxy.getRequestCompleted += new EventHandler<getRequestCompletedEventArgs>(proxy_getRequestCompleted_INIT);
                proxy.getRequestAsync(currentRequest.requestID, 0);
            }
        }
        void proxy_getRequestCompleted_INIT(object sender, getRequestCompletedEventArgs e)
        {
            if (e.Result.Status.Equals("Provisioned Windows 7"))
            {
                currentRequest = e.Result;
                Application.Current.Resources.Remove("Current Request");
                Application.Current.Resources.Add("Current Request", currentRequest);

                GranitePackagingServiceSoapClient proxy = new GranitePackagingServiceSoapClient();
                proxy.InnerChannel.OperationTimeout = new TimeSpan(0, 5, 0);
                proxy.getQAVMNameCompleted += new EventHandler<getQAVMNameCompletedEventArgs>(proxy_getQAVMNameCompleted_Win7_INIT);
                proxy.getQAVMNameAsync(currentRequest);
            }
            else if (e.Result.Status.Equals("Provisioned Windows XP"))
            {
                currentRequest = e.Result;
                Application.Current.Resources.Remove("Current Request");
                Application.Current.Resources.Add("Current Request", currentRequest);

                GranitePackagingServiceSoapClient proxy = new GranitePackagingServiceSoapClient();
                proxy.InnerChannel.OperationTimeout = new TimeSpan(0, 5, 0);
                proxy.getQAVMNameCompleted += new EventHandler<getQAVMNameCompletedEventArgs>(proxy_getQAVMNameCompleted_WinXP_INIT);
                proxy.getQAVMNameAsync(currentRequest);
            }
            else if (e.Result.Status.Equals("No Windows 7 Virtual Machines Available"))
            {
                MessageBox.Show("No Windows 7 Virtual Machines Available Please try later");

                currentRequest = e.Result;
                currentRequest.Status = "Pending";
                Application.Current.Resources.Remove("Current Request");
                Application.Current.Resources.Add("Current Request", currentRequest);

                GranitePackagingServiceSoapClient proxy = new GranitePackagingServiceSoapClient();
                proxy.InnerChannel.OperationTimeout = new TimeSpan(0, 5, 0);

                proxy.saveRequestCompleted += new EventHandler<saveRequestCompletedEventArgs>(proxy_saveRequestCompleted_INIT);
                proxy.saveRequestAsync(currentRequest);
            }
            else if (e.Result.Status.Equals("No Windows XP Virtual Machines Available"))
            {
                MessageBox.Show("No Windows 7 Virtual Machines Available Please try later");

                currentRequest = e.Result;
                currentRequest.Status = "Pending";
                Application.Current.Resources.Remove("Current Request");
                Application.Current.Resources.Add("Current Request", currentRequest);

                GranitePackagingServiceSoapClient proxy = new GranitePackagingServiceSoapClient();
                proxy.InnerChannel.OperationTimeout = new TimeSpan(0, 5, 0);

                proxy.saveRequestCompleted += new EventHandler<saveRequestCompletedEventArgs>(proxy_saveRequestCompleted_INIT);
                proxy.saveRequestAsync(currentRequest);
            }
            else if (e.Result.Stage.Equals("Deployment"))
            {
                busyIndicatorMain.IsBusy = false;

                currentRequest = e.Result;
                Application.Current.Resources.Remove("Current Request");
                Application.Current.Resources.Add("Current Request", currentRequest);

                this.NavigationService.Navigate(new Uri("/Home", UriKind.Relative));
            }
            else
            {
                busyIndicatorMain.BusyContent = e.Result.Status;
                GranitePackagingServiceSoapClient proxy = new GranitePackagingServiceSoapClient();
                proxy.InnerChannel.OperationTimeout = new TimeSpan(0, 5, 0);

                proxy.getRequestCompleted += new EventHandler<getRequestCompletedEventArgs>(proxy_getRequestCompleted_INIT);
                proxy.getRequestAsync(currentRequest.requestID, 5);
            }
        }

        void proxy_getQAVMNameCompleted_WinXP_INIT(object sender, getQAVMNameCompletedEventArgs e)
        {
            busyIndicatorMain.IsBusy = false;
            txtVMName.Text = e.Result.ToString();
            toggleWinXPTest();
        }

        void proxy_getQAVMNameCompleted_Win7_INIT(object sender, getQAVMNameCompletedEventArgs e)
        {
            busyIndicatorMain.IsBusy = false;
            txtVMName.Text = e.Result.ToString();
            toggleWin7Test();
        }
        void proxy_saveRequestCompleted_INIT(object sender, saveRequestCompletedEventArgs e)
        {
            busyIndicatorMain.IsBusy = false;
        }
        void proxy_getRequestWorkLogCompleted_INIT(object sender, getRequestWorkLogCompletedEventArgs e)
        {
            busyIndicatorMain.IsBusy = false;
            workLog = new ObservableCollection<WorkLogEntry>();
            if (e.Result.Count > 0)
            {
                foreach (WorkLogEntry entry in e.Result)
                {
                    WorkLogEntry modifiedEntry = entry;
                    modifiedEntry.NoteDate = modifiedEntry.NoteDate.ToLocalTime();

                    workLog.Add(modifiedEntry);
                }
            }
            gridWorkLog.ItemsSource = workLog;
        }
        #endregion
        #region worklog
        private void gridWorkLog_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            WorkLogEntry entry = (WorkLogEntry)gridWorkLog.SelectedItem;
            if (entry != null)
            {
                WorkLogEntryDetails childWindow = new WorkLogEntryDetails(entry);
                childWindow.boxWorkLogNotes.IsReadOnly = true;
                childWindow.Show();
            }
        }
        private void btnAddWorklogEntry_Click(object sender, RoutedEventArgs e)
        {
            Web.User user = WebContext.Current.User;

            GranitePackagingServiceSoapClient proxy = new GranitePackagingServiceSoapClient();
            proxy.getUserFriendlyNameCompleted += new EventHandler<getUserFriendlyNameCompletedEventArgs>(proxy_getUserFriendlyNameCompleted_AddWorkLogEntry);
            proxy.getUserFriendlyNameAsync(user.Name.Split('\\')[1].ToString(), user.Name.Split('\\')[0].ToString());
            busyIndicatorMain.BusyContent = "Creating New Work Log Entry";
            busyIndicatorMain.IsBusy = true;
        }
        void proxy_getUserFriendlyNameCompleted_AddWorkLogEntry(object sender, getUserFriendlyNameCompletedEventArgs e)
        {
            busyIndicatorMain.IsBusy = false;
            WorkLogEntry entry = new WorkLogEntry()
            {
                Note = String.Empty,
                NoteDate = DateTime.UtcNow,
                RequestID = currentRequest.requestID,
                UserName = e.Result.ToString(),
                WorkLogID = System.Guid.NewGuid().ToString()
            };

            WorkLogEntryDetails childWindow = new WorkLogEntryDetails(entry);
            childWindow.Closed += new EventHandler(childWindow_Closed_WorkLogEntry);

            childWindow.Show();
        }
        void childWindow_Closed_WorkLogEntry(object sender, EventArgs e)
        {
            WorkLogEntryDetails childWindow = (WorkLogEntryDetails)sender;
            if (childWindow.DialogResult.Value == true)
            {
                busyIndicatorMain.IsBusy = true;
                busyIndicatorMain.BusyContent = "Saving Work Log Entry";
                GranitePackagingServiceSoapClient proxy = new GranitePackagingServiceSoapClient();
                proxy.createWorkLogEntryCompleted += new EventHandler<createWorkLogEntryCompletedEventArgs>(proxy_createWorkLogEntryCompleted);
                proxy.createWorkLogEntryAsync(childWindow.entry.UserName, childWindow.entry.RequestID, childWindow.entry.Note, childWindow.entry.NoteDate);
            }
        }
        void proxy_createWorkLogEntryCompleted(object sender, createWorkLogEntryCompletedEventArgs e)
        {
            busyIndicatorMain.IsBusy = false;
            WorkLogEntry modifiedWorkLogEntry = e.Result;
            modifiedWorkLogEntry.NoteDate = modifiedWorkLogEntry.NoteDate.ToLocalTime();
            workLog.Add(modifiedWorkLogEntry);
        }
        #endregion
        
        #region win7 Testing
        private void toggleWin7Test()
        {
            if (btnTestWin7.IsEnabled)
            {
                btnTestWin7.IsEnabled = false;
                btnFinishedQA.IsEnabled = false;
                btnTestWinXP.IsEnabled = false;

                btnWin7Failure.IsEnabled = true;
                btnWin7Success.IsEnabled = true;

                panelVMName.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                btnTestWin7.IsEnabled = true;
                btnFinishedQA.IsEnabled = true;
                btnTestWinXP.IsEnabled = true;

                btnWin7Failure.IsEnabled = false;
                btnWin7Success.IsEnabled = false;

                panelVMName.Visibility = System.Windows.Visibility.Collapsed;
                txtVMName.Text = string.Empty;
            }
        }
        private void btnTestWin7_Click(object sender, RoutedEventArgs e)
        {
            toggleWin7Test();
            GranitePackagingServiceSoapClient proxy = new GranitePackagingServiceSoapClient();
            proxy.InnerChannel.OperationTimeout = new TimeSpan(0, 15, 0);

            proxy.RequestVirtualQAResourceCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(proxy_RequestVirtualQAResourceCompleted_Win7);
            proxy.RequestVirtualQAResourceAsync("Windows 7", currentRequest);

            busyIndicatorPackageInformation.IsBusy = true;
            busyIndicatorPackageInformation.BusyContent = "Requesting Windows 7 Test Machine";
        }
        void proxy_RequestVirtualQAResourceCompleted_Win7(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            GranitePackagingServiceSoapClient proxy = new GranitePackagingServiceSoapClient();
            proxy.InnerChannel.OperationTimeout = new TimeSpan(0, 5, 0);

            proxy.getRequestCompleted += new EventHandler<getRequestCompletedEventArgs>(proxy_getRequestCompleted_Win7Provision);
            proxy.getRequestAsync(currentRequest.requestID, 0);
            
        }
        void proxy_getRequestCompleted_Win7Provision(object sender, getRequestCompletedEventArgs e)
        {
            if (e.Result.Status.Equals("Provisioned Windows 7"))
            {
                

                currentRequest = e.Result;
                Application.Current.Resources.Remove("Current Request");
                Application.Current.Resources.Add("Current Request", currentRequest);

                GranitePackagingServiceSoapClient proxy = new GranitePackagingServiceSoapClient();
                proxy.InnerChannel.OperationTimeout = new TimeSpan(0, 5, 0);
                proxy.getQAVMNameCompleted += new EventHandler<getQAVMNameCompletedEventArgs>(proxy_getQAVMNameCompleted_Win7);
                proxy.getQAVMNameAsync(currentRequest);

            
                System.Windows.Browser.HtmlPage.Window.Navigate(new Uri("https://vdi.genmills.com"), "_newWindow");
            }
            else if (e.Result.Status.Equals("No Windows 7 Virtual Machines Available"))
            {
                MessageBox.Show("No Windows 7 Virtual Machines Available Please try later");
                
                currentRequest = e.Result;
                currentRequest.Status = "Pending";
                Application.Current.Resources.Remove("Current Request");
                Application.Current.Resources.Add("Current Request", currentRequest);

                GranitePackagingServiceSoapClient proxy = new GranitePackagingServiceSoapClient();
                proxy.InnerChannel.OperationTimeout = new TimeSpan(0, 5, 0);

                proxy.saveRequestCompleted += new EventHandler<saveRequestCompletedEventArgs>(proxy_saveRequestCompleted_Win7Provision_NoVMS);
                proxy.saveRequestAsync(currentRequest);
            }
            else
            {
                busyIndicatorPackageInformation.BusyContent = e.Result.Status;
                GranitePackagingServiceSoapClient proxy = new GranitePackagingServiceSoapClient();
                proxy.InnerChannel.OperationTimeout = new TimeSpan(0, 5, 0);

                proxy.getRequestCompleted += new EventHandler<getRequestCompletedEventArgs>(proxy_getRequestCompleted_Win7Provision);
                proxy.getRequestAsync(currentRequest.requestID, 5);
            }
        }
        void proxy_getQAVMNameCompleted_Win7(object sender, getQAVMNameCompletedEventArgs e)
        {
            txtVMName.Text = e.Result.ToString();
            busyIndicatorPackageInformation.IsBusy = false;
        }
        void proxy_saveRequestCompleted_Win7Provision_NoVMS(object sender, saveRequestCompletedEventArgs e)
        {
            busyIndicatorPackageInformation.IsBusy = false;
            toggleWin7Test();
        }
        private void btnWin7Success_Click(object sender, RoutedEventArgs e)
        {
            btnWin7Failure.IsEnabled = false;
            btnWin7Success.IsEnabled = false;

            spinnerWin7.Visibility = System.Windows.Visibility.Visible;
            imgWin7NonCompat.Visibility = System.Windows.Visibility.Collapsed;
            imgWin7Compat.Visibility = System.Windows.Visibility.Collapsed;

            GranitePackagingServiceSoapClient proxy = new GranitePackagingServiceSoapClient();
            proxy.InnerChannel.OperationTimeout = new TimeSpan(0, 5, 0);

            programData.supportWin7_64 = true;

            proxy.saveProgramCompleted += new EventHandler<saveProgramCompletedEventArgs>(proxy_saveProgramCompleted_SuccessWin7);
            proxy.saveProgramAsync(programData);

            proxy.UnprovisionVirtualQAResourceAsync(currentRequest);
        }
        void proxy_saveProgramCompleted_SuccessWin7(object sender, saveProgramCompletedEventArgs e)
        {
            spinnerWin7.Visibility = System.Windows.Visibility.Collapsed;
            imgWin7NonCompat.Visibility = System.Windows.Visibility.Collapsed;
            imgWin7Compat.Visibility = System.Windows.Visibility.Visible;

            currentRequest.Status = "Pending";

            Application.Current.Resources.Remove("Current Request");
            Application.Current.Resources.Add("Current Request", currentRequest);

            GranitePackagingServiceSoapClient proxy = new GranitePackagingServiceSoapClient();
            proxy.InnerChannel.OperationTimeout = new TimeSpan(0, 5, 0);

            proxy.saveRequestCompleted += new EventHandler<saveRequestCompletedEventArgs>(proxy_saveRequestCompleted_Win7);
            proxy.saveRequestAsync(currentRequest);
        }
        void proxy_saveRequestCompleted_Win7(object sender, saveRequestCompletedEventArgs e)
        {
            toggleWin7Test();
        }
        private void btnWin7Failure_Click(object sender, RoutedEventArgs e)
        {
            btnWin7Failure.IsEnabled = false;
            btnWin7Success.IsEnabled = false;

            spinnerWin7.Visibility = System.Windows.Visibility.Visible;
            imgWin7NonCompat.Visibility = System.Windows.Visibility.Collapsed;
            imgWin7Compat.Visibility = System.Windows.Visibility.Collapsed;

            GranitePackagingServiceSoapClient proxy = new GranitePackagingServiceSoapClient();
            proxy.InnerChannel.OperationTimeout = new TimeSpan(0, 5, 0);

            programData.supportWin7_64 = false;

            proxy.saveProgramCompleted += new EventHandler<saveProgramCompletedEventArgs>(proxy_saveProgramCompleted_FailureWin7);
            proxy.saveProgramAsync(programData);

            proxy.UnprovisionVirtualQAResourceAsync(currentRequest);
        }
        void proxy_saveProgramCompleted_FailureWin7(object sender, saveProgramCompletedEventArgs e)
        {
            spinnerWin7.Visibility = System.Windows.Visibility.Collapsed;
            imgWin7NonCompat.Visibility = System.Windows.Visibility.Visible;
            imgWin7Compat.Visibility = System.Windows.Visibility.Collapsed;

            currentRequest.Status = "Pending";

            Application.Current.Resources.Remove("Current Request");
            Application.Current.Resources.Add("Current Request", currentRequest);

            GranitePackagingServiceSoapClient proxy = new GranitePackagingServiceSoapClient();
            proxy.InnerChannel.OperationTimeout = new TimeSpan(0, 5, 0);

            proxy.saveRequestCompleted += new EventHandler<saveRequestCompletedEventArgs>(proxy_saveRequestCompleted_Win7);
            proxy.saveRequestAsync(currentRequest);
        }
        #endregion
        #region Win XP Testing
        private void toggleWinXPTest()
        {
            if (btnTestWinXP.IsEnabled)
            {
                btnTestWin7.IsEnabled = false;
                btnFinishedQA.IsEnabled = false;
                btnTestWinXP.IsEnabled = false;

                btnWinXPFailure.IsEnabled = true;
                btnWinXPSuccess.IsEnabled = true;

                panelVMName.Visibility = System.Windows.Visibility.Visible;
            }
            else
            {
                btnTestWin7.IsEnabled = true;
                btnFinishedQA.IsEnabled = true;
                btnTestWinXP.IsEnabled = true;

                btnWinXPFailure.IsEnabled = false;
                btnWinXPSuccess.IsEnabled = false;

                panelVMName.Visibility = System.Windows.Visibility.Collapsed;
                txtVMName.Text = string.Empty;
            }
        }
        private void btnTestWinXP_Click(object sender, RoutedEventArgs e)
        {
            toggleWinXPTest();
            GranitePackagingServiceSoapClient proxy = new GranitePackagingServiceSoapClient();
            proxy.InnerChannel.OperationTimeout = new TimeSpan(0, 15, 0);

            proxy.RequestVirtualQAResourceCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(proxy_RequestVirtualQAResourceCompleted_WinXP);
            proxy.RequestVirtualQAResourceAsync("Windows XP", currentRequest);

            busyIndicatorPackageInformation.IsBusy = true;
            busyIndicatorPackageInformation.BusyContent = "Requesting Windows XP Test Machine";
        }
        void proxy_RequestVirtualQAResourceCompleted_WinXP(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            GranitePackagingServiceSoapClient proxy = new GranitePackagingServiceSoapClient();
            proxy.InnerChannel.OperationTimeout = new TimeSpan(0, 5, 0);

            proxy.getRequestCompleted += new EventHandler<getRequestCompletedEventArgs>(proxy_getRequestCompleted_WinXPProvision);
            proxy.getRequestAsync(currentRequest.requestID, 0);
        }
        void proxy_getRequestCompleted_WinXPProvision(object sender, getRequestCompletedEventArgs e)
        {
            if (e.Result.Status.Equals("Provisioned Windows XP"))
            {
               

                currentRequest = e.Result;
                Application.Current.Resources.Remove("Current Request");
                Application.Current.Resources.Add("Current Request", currentRequest);

                GranitePackagingServiceSoapClient proxy = new GranitePackagingServiceSoapClient();
                proxy.InnerChannel.OperationTimeout = new TimeSpan(0, 5, 0);
                proxy.getQAVMNameCompleted += new EventHandler<getQAVMNameCompletedEventArgs>(proxy_getQAVMNameCompleted_WinXP);
                proxy.getQAVMNameAsync(currentRequest);

                System.Windows.Browser.HtmlPage.Window.Navigate(new Uri("https://vdi.genmills.com"), "_newWindow");
            }
            else if (e.Result.Status.Equals("No Windows XP Virtual Machines Available"))
            {
                MessageBox.Show("No Windows XP Virtual Machines Available Please try later");
                
                currentRequest = e.Result;
                currentRequest.Status = "Pending";
                Application.Current.Resources.Remove("Current Request");
                Application.Current.Resources.Add("Current Request", currentRequest);

                GranitePackagingServiceSoapClient proxy = new GranitePackagingServiceSoapClient();
                proxy.InnerChannel.OperationTimeout = new TimeSpan(0, 5, 0);

                proxy.saveRequestCompleted += new EventHandler<saveRequestCompletedEventArgs>(proxy_saveRequestCompleted_WinXPNoVMs);
                proxy.saveRequestAsync(currentRequest);
            }
            else
            {
                busyIndicatorPackageInformation.BusyContent = e.Result.Status;
                GranitePackagingServiceSoapClient proxy = new GranitePackagingServiceSoapClient();
                proxy.InnerChannel.OperationTimeout = new TimeSpan(0, 5, 0);

                proxy.getRequestCompleted += new EventHandler<getRequestCompletedEventArgs>(proxy_RequestVirtualQAResourceCompleted_WinXP);
                proxy.getRequestAsync(currentRequest.requestID, 5);
            }
        }
        void proxy_getQAVMNameCompleted_WinXP(object sender, getQAVMNameCompletedEventArgs e)
        {
            txtVMName.Text = e.Result.ToString();
            
            busyIndicatorPackageInformation.IsBusy = false;
        }
        void proxy_saveRequestCompleted_WinXPNoVMs(object sender, saveRequestCompletedEventArgs e)
        {
            busyIndicatorPackageInformation.IsBusy = false;
            toggleWinXPTest();
        }
        private void btnWinXPSuccess_Click(object sender, RoutedEventArgs e)
        {
            btnWinXPFailure.IsEnabled = false;
            btnWinXPSuccess.IsEnabled = false;

            spinnerWinXP.Visibility = System.Windows.Visibility.Visible;
            imgWinXPNonCompat.Visibility = System.Windows.Visibility.Collapsed;
            imgWinXPCompat.Visibility = System.Windows.Visibility.Collapsed;

            GranitePackagingServiceSoapClient proxy = new GranitePackagingServiceSoapClient();
            proxy.InnerChannel.OperationTimeout = new TimeSpan(0, 5, 0);

            programData.supportWinXP_32 = true;

            proxy.saveProgramCompleted += new EventHandler<saveProgramCompletedEventArgs>(proxy_saveProgramCompleted_SuccessWinXP);
            proxy.saveProgramAsync(programData);

            proxy.UnprovisionVirtualQAResourceAsync(currentRequest);
        }
        void proxy_saveProgramCompleted_SuccessWinXP(object sender, saveProgramCompletedEventArgs e)
        {
            spinnerWinXP.Visibility = System.Windows.Visibility.Collapsed;
            imgWinXPNonCompat.Visibility = System.Windows.Visibility.Collapsed;
            imgWinXPCompat.Visibility = System.Windows.Visibility.Visible;

            currentRequest.Status = "Pending";

            Application.Current.Resources.Remove("Current Request");
            Application.Current.Resources.Add("Current Request", currentRequest);

            GranitePackagingServiceSoapClient proxy = new GranitePackagingServiceSoapClient();
            proxy.InnerChannel.OperationTimeout = new TimeSpan(0, 5, 0);

            proxy.saveRequestCompleted += new EventHandler<saveRequestCompletedEventArgs>(proxy_saveRequestCompleted_WinXP);
            proxy.saveRequestAsync(currentRequest);
        }

        void proxy_saveRequestCompleted_WinXP(object sender, saveRequestCompletedEventArgs e)
        {
            toggleWinXPTest();
        }
        private void btnWinXPFailure_Click(object sender, RoutedEventArgs e)
        {
            btnWinXPFailure.IsEnabled = false;
            btnWinXPSuccess.IsEnabled = false;

            spinnerWinXP.Visibility = System.Windows.Visibility.Visible;
            imgWinXPNonCompat.Visibility = System.Windows.Visibility.Collapsed;
            imgWinXPCompat.Visibility = System.Windows.Visibility.Collapsed;

            GranitePackagingServiceSoapClient proxy = new GranitePackagingServiceSoapClient();
            proxy.InnerChannel.OperationTimeout = new TimeSpan(0, 5, 0);

            programData.supportWinXP_32 = false;

            proxy.saveProgramCompleted += new EventHandler<saveProgramCompletedEventArgs>(proxy_saveProgramCompleted_FailureWinXP);
            proxy.saveProgramAsync(programData);

            proxy.UnprovisionVirtualQAResourceAsync(currentRequest);
        }
        void proxy_saveProgramCompleted_FailureWinXP(object sender, saveProgramCompletedEventArgs e)
        {
            spinnerWinXP.Visibility = System.Windows.Visibility.Collapsed;
            imgWinXPNonCompat.Visibility = System.Windows.Visibility.Visible;
            imgWinXPCompat.Visibility = System.Windows.Visibility.Collapsed;
           
            currentRequest.Status = "Pending";

            Application.Current.Resources.Remove("Current Request");
            Application.Current.Resources.Add("Current Request", currentRequest);

            GranitePackagingServiceSoapClient proxy = new GranitePackagingServiceSoapClient();
            proxy.InnerChannel.OperationTimeout = new TimeSpan(0, 5, 0);

            proxy.saveRequestCompleted += new EventHandler<saveRequestCompletedEventArgs>(proxy_saveRequestCompleted_WinXP);
            proxy.saveRequestAsync(currentRequest);
        }
        #endregion
        #region Refresh SCCM Source Files
        private void btnRefreshSourceFiles_Click(object sender, RoutedEventArgs e)
        {
            GranitePackagingServiceSoapClient proxy = new GranitePackagingServiceSoapClient();
            proxy.InnerChannel.OperationTimeout = new TimeSpan(0, 5, 0);

            busyIndicatorMain.BusyContent = "Refreshing Package Source Files";
            busyIndicatorMain.IsBusy = true;
            
            proxy.RefreshSCCMPackageSourceFilesCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(proxy_RefreshSCCMPackageSourceFilesCompleted);
            proxy.RefreshSCCMPackageSourceFilesAsync(currentRequest.SCCMPackageID);
        }
        void proxy_RefreshSCCMPackageSourceFilesCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            busyIndicatorMain.IsBusy = false;
        }
        #endregion  
        
        #region finish QA
        private void btnFinishedQA_Click(object sender, RoutedEventArgs e)
        {
            busyIndicatorMain.IsBusy = true;
            busyIndicatorMain.BusyContent = "Saving Request";
            GranitePackagingServiceSoapClient proxy = new GranitePackagingServiceSoapClient();
            proxy.InnerChannel.OperationTimeout = new TimeSpan(0, 5, 0);

            proxy.finishQAStageCompleted += new EventHandler<System.ComponentModel.AsyncCompletedEventArgs>(proxy_finishQAStageCompleted);
            proxy.finishQAStageAsync(currentRequest);
        }
        void proxy_finishQAStageCompleted(object sender, System.ComponentModel.AsyncCompletedEventArgs e)
        {
            GranitePackagingServiceSoapClient proxy = new GranitePackagingServiceSoapClient();
            proxy.InnerChannel.OperationTimeout = new TimeSpan(0, 5, 0);

            proxy.getRequestCompleted += new EventHandler<getRequestCompletedEventArgs>(proxy_getRequestCompleted_FinishQA);
            proxy.getRequestAsync(currentRequest.requestID, 0);
        }
        void proxy_getRequestCompleted_FinishQA(object sender, getRequestCompletedEventArgs e)
        {
            if (e.Result.Stage.Equals("QA"))
            {
                busyIndicatorMain.BusyContent = e.Result.Status;
                GranitePackagingServiceSoapClient proxy = new GranitePackagingServiceSoapClient();
                proxy.InnerChannel.OperationTimeout = new TimeSpan(0, 5, 0);

                proxy.getRequestCompleted += new EventHandler<getRequestCompletedEventArgs>(proxy_RequestVirtualQAResourceCompleted_WinXP);
                proxy.getRequestAsync(currentRequest.requestID, 5);        
            }
            else
            {
                busyIndicatorMain.IsBusy = false;

                currentRequest = e.Result;
                Application.Current.Resources.Remove("Current Request");
                Application.Current.Resources.Add("Current Request", currentRequest);
                busyIndicatorMain.IsBusy = false;
                this.NavigationService.Navigate(new Uri("/Home", UriKind.Relative));       
            }
        }
        #endregion
    }
}
